package com.cxhome.cxinfox.order;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.traffic.managent.annotation.ModelBind;
import com.traffic.managent.base.SessionUser;

@ModelBind(table = "cx_Order", key = "ids")
public class Order extends SessionUser<Order> {

	private static final long serialVersionUID = 1L;
	public static final Order dao = new Order();

	public Order orderCount(Map<String, Object> params) {

		StringBuilder sql = new StringBuilder(
				"SELECT o.* FROM cx_order o LEFT JOIN cx_pay_record p ON o.orderNo = p.order_no "
						+ " LEFT JOIN cx_customer_info c ON o.customerId = c.customerId "
						+ " LEFT JOIN cx_provider pd ON o.providerId = pd.ids " + "WHERE o.payMode = '1'");

		sql = buildSQL(sql, params);
		return dao.findFirst(sql.toString());
	}

	public List<Order> orderList(Map<String, Object> params) {
		StringBuilder sql = new StringBuilder(
				"SELECT " + " o.ids,   o.orderNo,   o.phoneNo,   o.vasProdName,  o.sellPrice, "
						+ " o.cstmOrderNo, case when o.customerDiscount is null then o.sellPrice else "
						+ " ROUND(o.sellPrice/(o.customerDiscount),2) end productPrice, "
						+ " case when o.orderTime is null then '' else "
						+ " DATE_FORMAT(o.orderTime,'%Y-%m-%d %H:%i:%s') end orderTime, "
						+ " case when o.openTime is null then '' else "
						+ " DATE_FORMAT(o.openTime,'%Y-%m-%d %H:%i:%s') end openTime,  CASE "
						+ " WHEN o. STATUS = '0' THEN '受理中'   WHEN o. STATUS = '1' THEN   '订购中' "
						+ " WHEN o. STATUS = '3' THEN   '鉴权失败'   WHEN o. STATUS = '7' THEN   '订购成功' "
						+ " WHEN o. STATUS = '8' THEN   '订购失败'   ELSE   '未知状态'   END STATUS, "
						+ " c.customerName,   o.vasProdName,   o.payMode,   o.currentAmount, "
						+ " case when p.channel is null then '' else p.channel end payChannel, "
						+ " case when p.transaction_no is null then '' else p.transaction_no end   transactionNo, "
						+ " case when p.id is null then '' else p.id end chargeId,case when "
						+ " o.message is null then '' else o.message end message, "
						+ " case when pd.providerName is null then '' else pd.providerName end   providerName, "
						+ " case when o.errorMsg is null then '' else o.errorMsg end errorMsg   FROM " + " cx_order o "
						+ " LEFT JOIN cx_pay_record p ON o.orderNo = p.order_no "
						+ " LEFT JOIN cx_customer_info c ON o.customerId = c.customerId "
						+ " LEFT JOIN cx_provider pd ON o.providerId = pd.ids  WHERE 1 = 1 ");
		sql = buildSQL(sql, params);

		String filterAuthOrder = (String) params.get("filterAuthOrder");
//		if (StringUtils.isNotEmpty(filterAuthOrder)) {
//			sql.append("and o.status <> '3' ");
//		}
		String dataIds = (String) params.get("dataIds");
		if (StringUtils.isNotEmpty(dataIds)) {
			sql.append("and o.customerId in " + dataIds);
		}
		int start = (Integer) params.get("start");
		int end = (Integer) params.get("end");

		if (start != 0 && end != 0) {
			sql.append(" order by o.orderTime asc limit " + start + "," + end);
		}

		return dao.find(sql.toString());
	}

	public static StringBuilder buildSQL(StringBuilder sql, Map<String, Object> params) {

		String status = (String) params.get("status");
		if (StringUtils.isNotEmpty(status)) {
			sql.append(" and  o.status = '" + status + "'");
		}
		String departmentId = (String) params.get("departmentId");
		if (StringUtils.isNotEmpty(departmentId)) {
			sql.append(" AND o.department in " + departmentId);
		}
		String payMode = (String) params.get("payMode");
		if (StringUtils.isNotEmpty(payMode)) {
			sql.append(" and  o.payMode = '" + payMode + "' ");
		}
		String customerId = (String) params.get("customerId");
		if (StringUtils.isNotEmpty(customerId)) {
			sql.append(" and  o.customerId = '" + customerId + "' ");
		}
		String ids = (String) params.get("ids");
		if (StringUtils.isNotEmpty(ids)) {
			sql.append(" and  o.ids = '" + ids + "' ");
		}
		String orderNo = (String) params.get("orderNo");
		if (StringUtils.isNotEmpty(orderNo)) {
			sql.append(" and  o.orderNo like '%" + orderNo + "%'");
		}
		String phoneNo = (String) params.get("phoneNo");
		if (StringUtils.isNotEmpty(phoneNo)) {
			sql.append(" and  o.phoneNo like '%" + phoneNo + "%'");
		}
		String customerName = (String) params.get("customerName");
		if (StringUtils.isNotEmpty(customerName)) {
			sql.append(" and  c.customerName like '%" + customerName + "%'");
		}
		String operator = (String) params.get("operator");
		if (StringUtils.isNotEmpty(operator)) {
			sql.append(" and  o.operator = '" + operator + "'");
		}
		String providerId = (String) params.get("providerId");
		if (StringUtils.isNotEmpty(providerId)) {
			sql.append(" and  o.providerId = '" + providerId + "'");
		}
		if (params.get("startTime") != null && !"".equals(params.get("startTime"))) {
			sql.append(" and  o.orderTime >= '" + params.get("startTime") + "'");
		}
		if (params.get("endTime") != null && !"".equals(params.get("endTime"))) {
			sql.append(" and  o.orderTime <= '" + params.get("endTime") + "'");
		}
		return sql;
	}
}
